<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Welcome extends CI_Controller {
        function __construct()
        {
            parent::__construct();
            $this->load->model('modelo');
        }
	public function index()
	{
            $this->load->view('header2');
            if($this->session->userdata('username') == null)
                $nombres['nombre']=" a Activanet";
            else
                $nombres['nombre']= $this->session->userdata('username');
            $this->load->view('principal',$nombres);
            $this->load->view('footer2');
	}
        function login2()
        {
            $this->load->view('header2');
            if($this->session->userdata('username') == null)
                $nombres['nombre']=" a Activanet";
            else
                $nombres['nombre']= $this->session->userdata('username');
            $this->load->view('principal',$nombres);
            $this->load->view('footer2');
        }
        function login()
        {
            $nickname = $this->input->post('nickname');
            echo $nickname;
            $clave = $this->input->post('clave');
            
            $numero = $this->modelo->consulta($nickname,$clave)->num_rows();
            $msj="false";
            if($numero >0):
                $permiso = $this->modelo->consultaPermiso($nickname,$clave)->result();
                foreach($permiso as $row):
                    $permiso = $row->pf_cod;
                endforeach;
                $data   =   array(
                    'username'  => $nickname,
                    'logged_in' => TRUE,
                    'permiso' => $permiso
                );
                $this->session->set_userdata($data);
                $msj="true";
            endif;
            $data['nombre']=$nickname;
            $this->load->view('principal2',$data);
            echo json_encode(array("msj"=>$msj));
        }
        function cargaPrincipal()
        {
            if($this->session->userdata('permiso')=='1'):
                $datas['nombre'] = $this->session->userdata('username');
                $this->load->view('principal2',$datas);
                //$this->load->view('registro',$datas);
                
            else:
                if($this->session->userdata('username') == null)
                    $nombres['nombre']=" a Activanet";
                else
                    $nombres['nombre']= $this->session->userdata('username');
                $this->load->view('principal2',$nombres);
            endif;
        }
        function verificaLogin()
        {
            if($this->session->userdata('logged_in')==TRUE):
                $msj = 'true';
            else:
                $msj = 'false';
            endif;
           // echo json_encode(array('msje'=>$msj));
        }
        function logout()
        {
            $this->session->sess_destroy();
            redirect(base_url());
        }
        function guardaRegistro()
        {
            $nombre = $this->input->post('per_nom');
            $rut    = $this->input->post('per_rut');
            $correo = $this->input->post('per_email');
            $nickname = $this->input->post('per_usu');
            $clave = md5($this->input->post('per_pass'));
            $permiso = $this->input->post('pf_cod');
            $this->modelo->guardaRegistro($nombre,$rut,$correo,$nickname,$clave,$permiso);
            $usuarios = $this->modelo->buscaUsuarios()->result();
            $i=0;
            foreach ($usuarios as $row):
                $data['per_nom'.$i] = $row->Nombre;
                $data['per_email'.$i] = $row->Correo;
                $data['per_rut'.$i] = $row->Rut;
                $i=$i+1;
            endforeach;
            $data['indice']=$i;
            $this->load->view('informe',$data);
            //echo $nombre.' '.$rut.' '.$correo.' '.$nickname.' '.$clave.' '.$permiso;
        }
        function registrar()
        {
            $usuarios = $this->modelo->buscaUsuario()->result();
            $i=0;
            foreach ($usuarios as $row):
                $datos['nickname'.$i] = $row->per_usu;
                $datos['clave'.$i] = $row->per_pass;
                $i++;
            endforeach;
            $datos["indice"]=$i;
            $this->load->view('registrar',$datos);
        }
        function comentario(){
            $nombre = $this->input->post('nombre');
            $empresa = $this->input->post('empresa');
            $email = $this->input->post('email');
            $fono = $this->input->post('fono');
            $comentario  = $this->input->post('comentario');
            
            //Enviar email;
            $msj = "Comentario enviado al administrador";
            echo json_encode(array("mensaje"=>$msj));
        }
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */
